package sparrowGui
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import sparrowGui.impl.IScrollPan;
	import sparrowGui.utils.PubFun;
	
	
	/**
	 * 滚动文本域,如果构造的时候tf参数是场景上的文本,则直接替换场上文本,不用addChild
	 * @author Pelephone
	 */	
	public class VTextArea extends ComponentDraw implements IScrollPan
	{
		private var _tf:TextField;			// 文本控件
		private var _bar:VScrollBar;		// 滚动条控件
		private var _replace:Boolean;		// 替换掉原有文本域
		private var _scrollValue:Number;	//暂存滚动条位置,当style.isSaveSelect为真时用到
		
		/**
		 * @param tf			如果构造的时候tf参数是场景上的文本,则直接替换场上文本,不用addChild
		 * @param scrollSkin	滚动条皮肤
		 * @param vars
		 */		
		public function VTextArea(tf:Object, scrollSkin:Sprite=null, vars:Object=null)
		{
			if(tf is TextField) _tf = tf as TextField;
			else{
				_tf = new TextField();
				_tf.multiline = true;
				addChild(_tf);
			}
			
			vars = vars || SparrowGUI.getIns().textAreaVar;
			_bar = new VScrollBar(vars.scrollSkin);
			
			super(vars);
			
			
			if(tf is TextField){
				setSize(_tf.width,_tf.height);
				PubFun.replace(this, _tf);
			}else update(tf);
		}
		
		override protected function init():void
		{
			this.addChild(_bar);
		}
		
		// 激活滚动条
		protected function activeScroll():void
		{
			_bar.width = VScrollBar.THICKNESS;
			_bar.height = style.height;

			// 通过遮罩激活滚动条
//			var step:Number = (contDP.height+style.spacing)/itemsData.length;
			var stepValue:Number = 1/(_tf.maxScrollV);
			_bar.setSliderParams((_tf.bottomScrollV-_tf.scrollV+1),_tf.numLines,stepValue);
			
			if(style.autoHide==1){
				_bar.visible = true;
			}
			else if(style.autoHide==2 || (!_bar.enabled && style.autoHide==0)){
				// 未滚动条未激活状态,autoHide为0,或者2则将其隐藏
				_bar.visible = false;
				return;
			}
			
			// 激活滚动条后才知道其是否显示,而后设置位置
			_bar.x = style.width - _bar.width - style.paddingRight;
			_bar.y = style.paddingTop;
			
			addEvt();
			
			// 设置上次保存的滚动条信息
			if(style.isSaveSelect && _scrollValue){
				_bar.value = _scrollValue;
				_bar.onMoveSlider(null);
			}
		}
		
		//添加滚动条事件
		private function addEvt():void
		{
			if((_tf.maxScrollV)>0){
				if(_bar.hasEventListener(Event.CHANGE)) return;
				_bar.addEventListener(Event.CHANGE,onEvt);
				_tf.addEventListener(Event.SCROLL,onEvt);
			}else
				delEvt();
		}
		
		private function delEvt():void
		{
			if(_bar) _bar.removeEventListener(Event.CHANGE,onEvt);
			removeEventListener(MouseEvent.MOUSE_WHEEL, onEvt);
			_tf.removeEventListener(Event.SCROLL,onEvt);
		}
		
		private function onEvt(e:Event):void
		{
			switch(e.type){
				case Event.CHANGE:
					_tf.removeEventListener(Event.SCROLL,onEvt);
					_tf.scrollV = Math.round(_bar.value*(_tf.maxScrollV-1))+1;
					_tf.addEventListener(Event.SCROLL,onEvt);
					break;
				case Event.SCROLL:
					_bar.value = (_tf.scrollV-1)/(_tf.maxScrollV-1);
					break;
			}
		}
		
		override protected function draw():void
		{
			super.draw();
			
			activeScroll();
			
			_bar.x = style.width - _bar.width;
			_tf.width = style.width - (_bar.visible?_bar.width:0);
			_tf.height = style.height;
		}
		
		public function update(data:Object):void
		{
			_tf.htmlText = String(data);
			nextDraw();
		}
		
		public function setScrollIndex(index:int):void
		{
			_tf.scrollV = index;
		}
		
		public function get panHeight():Number
		{
			return height;
		}
		
		public function get tf():TextField
		{
			return _tf;
		}

		override public function dispose():void
		{
			delEvt();
			super.dispose();
			_tf.parent.removeChild(_tf);
			_bar.parent.removeChild(_bar);
			_tf = null;
			_bar = null;
		}
	}
}